<?php


namespace app\index\controller;


use app\common\Base;
use app\index\model\Business;
use app\index\model\Coupon;
use app\index\model\CouponReceive;
use app\index\model\Region;
use think\Exception;
use think\Request;

class CouponPage  extends Base
{
    /**
     * @param Request $request
     * @param Coupon $coupon
     * @param Business $business
     * @return \think\response\Json
     */
       //列分页优惠卷(成功)
    public function selCoupon(Request $request , Coupon $coupon,CouponReceive $couponReceive ,Business $business){
        // TODO 将获取缓存信息放在中间件里，请使用try catch，代码缩进不规范
        $tokenarr = $request->cacheInfo;
        $userId = $tokenarr['id'];//获取用户id
        $pagenum = $request->post('pagenum');//获取当前页数
        $place = $request->post('place');//获取地区筛选
        if($pagenum ==''){
            $pagenum=1;
        }
        $data = array();
        $Data = array();
        $text = array();
        //查询优惠卷
        $coupontext =json_decode( $coupon -> selallCoupon(),true);

        try{
            //循环出指定键
            for ($ii = 0;$ii<count($coupontext);$ii++){
                $businessId = $coupontext[$ii]['businessId'];
                //获取商家的地址等信息
                $businesstext =json_decode( $business->findBusinessadd($businessId),true);

                $businesstext=$businesstext[0];
                if ($place==''){
                    array_push($data, $coupontext[$ii]);

                        $data[count($data)-1]['business'] = $businesstext;

                }

                if ($businesstext['county'] ==$place){
                    array_push($data, $coupontext[$ii]);
                        $data[count($data)-1]['business'] = $businesstext;
                }

            }

            //每页起始键                   定义分页数据条数
            $start = $pagenum  * Base::$pageSize - Base::$pageSize;
            //每页结尾键
            $end = $pagenum * Base::$pageSize;


            //如果操过了，就按数量最后
            if($end > count($data)){
                $end = count($data);
            }
            for($i=$start;$i<$end;$i++){
                array_push($text,$data[$i]);
            }

            $page =intdiv(count($data),5) + 1 ;
            $Data['page']['pagenum'] = $page;
            $Data['page']['nowpage'] = $pagenum;
            $Data['shuju'] = $text;
            for($d=0;$d<count($Data['shuju']);$d++){
                $copid = json_decode($couponReceive->selCoupon($userId,$Data['shuju'][$d]['id']),true);
                if(!count($copid)==0){
                    $Data['shuju'][$d]['isready']= 1;
                }else{
                    $Data['shuju'][$d]['isready']= 0;
                }

            }

            return responseJson(Base::OK,'成功',$Data);
        }catch (Exception $e){
            throw $e;
        }

       
}
    /**
     * @param Request $request
     * @param CouponReceive $couponReceive
     * @return \think\response\Json
     */
       //领取优惠卷 (成功)
       public function  addCoupan( Request $request,Coupon $coupon,CouponReceive $couponReceive){
           // TODO 将获取缓存信息放在中间件里，请使用try catch，代码缩进不规范
           $tokenarr = $request->cacheInfo;
           $userId = $tokenarr['id'];//获取用户id

           $arr = $request->post();//获取所有数据
           $couponId = $request->post('couponId');//获取所有数据
           $time = json_decode($coupon->findCoupontime($couponId),true)[0]['starTime'];
           try{
               if($couponReceive->addCoupan($userId,$arr,$time)){
                   return responseJson(Base::OK,'添加成功');
               }else{
                   return responseJson(Base::FAIL,'服务器错误，请吃屎');
               } 
               // TODO 若以上添加操作失败后，就没有信息放回给前端？前端等了个寂寞？
           }catch (Exception $e){
               throw  $e;
           }
           
          
       }

       //地区列出来
    public function  add(Request $request ,Region $region){
       // TODO 请使用try catch
        try{
            $code = $request ->post('code');//获取地区城市code
            $data  = $region->selcounty($code);
            return responseJson(Base::OK,'列出成功', $data);
        }catch (\Exception $e){
            throw $e;
        }
        
    }
}